﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KinConquer.Packets.Structures
{
    public class MessageConst
    {
        public static Byte[]
            BannedAccount = new MessagePacket("This account is banned.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation),
            BannedAccountPERM = new MessagePacket("This account is banned permanently.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation),
            Banned = new MessagePacket("You are banned.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation),
            BannedPERM = new MessagePacket("You are banned permanently.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation),

            InvalidHeroName = new MessagePacket("Invalid Hero Name.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.Dialog),
            HeroExists = new MessagePacket("A Hero with that name already exists.", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.Dialog),

            ANSWER_OK = new MessagePacket("ANSWER_OK", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation),
            ANSWER_OK_Dialog = new MessagePacket("ANSWER_OK", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.Dialog),
            NEW_ROLE = new MessagePacket("NEW_ROLE", "ALLUSERS", "SYSTEM", 0xffffff, MessagePacket.Types.LoginInformation);
    }

    public unsafe struct MessagePacket
    {
        public enum Types : ushort
        {
            Talk = 2000,
            Whisper = 2001,
            Action = 2002,
            Team = 2003,
            Guild = 2004,
            Top = 2005,
            Clan = 2006,
            Yell = 2008,
            Friend = 2009,
            Center = 2011,
            Ghost = 2013,
            Service = 2014,
            Tip = 2015,
            World = 2021,
            Qualifier = 2022,
            Dialog = 2100,
            LoginInformation = 2101,
            VendorHawk = 2104,
            Website = 2105,
            MiniMap = 2108,
            MiniMap2 = 2109,
            FriendsOfflineMessage = 2110,
            GuildBulletin = 2111,
            TradeBoard = 2201,
            FriendBoard = 2202,
            TeamBoard = 2203,
            GuildBoard = 2204,
            OthersBoard = 2205,
            Broadcast = 2500,
        }

        public string Message, From, To;
        public uint dwParam;
        public uint Color;
        public Types ChatType;
        public uint SenderModel;

        public MessagePacket(string _Message, Types _ChatType)
        {
            Message = _Message;
            To = "ALL";
            From = "SYSTEM";
            Color = (UInt32)System.Drawing.Color.Red.ToArgb();
            ChatType = _ChatType;
            dwParam = SenderModel = 0;
        }

        public MessagePacket(string _Message, uint _Color, Types _ChatType)
        {
            Message = _Message;
            To = "ALL";
            From = "SYSTEM";
            Color = _Color;
            ChatType = _ChatType;
            dwParam = SenderModel = 0;
        }
        public MessagePacket(string _Message, string _To, uint _Color, Types _ChatType)
        {
            Message = _Message;
            To = _To;
            From = "SYSTEM";
            Color = _Color;
            ChatType = _ChatType;
            dwParam = SenderModel = 0;
        }
        public MessagePacket(string _Message, string _To, string _From, uint _Color, Types _ChatType)
        {
            Message = _Message;
            To = _To;
            From = _From;
            Color = _Color;
            ChatType = _ChatType;
            dwParam = SenderModel = 0;
        }
        public static implicit operator byte[](MessagePacket Msg)
        {
            byte[] Buffer = new byte[32 + Msg.Message.Length + Msg.From.Length + Msg.To.Length];
            fixed (byte* Packet = Buffer)
            {
                *((ushort*)(Packet)) = (ushort)(Buffer.Length);
                *((ushort*)(Packet + 2)) = 0x3EC;
                *((uint*)(Packet + 4)) = Msg.Color;
                *((Types*)(Packet + 8)) = Msg.ChatType;
                *((uint*)(Packet + 12)) = Msg.dwParam;
                *((uint*)(Packet + 20)) = Msg.SenderModel;
                Packet[24] = 0x04;

                Packet[25] = (byte)Msg.From.Length;
                Packet[26 + Msg.From.Length] = (byte)Msg.To.Length;
                Packet[28 + Msg.From.Length + Msg.To.Length] = (byte)Msg.Message.Length;

                Msg.From.CopyTo(Packet + 26);
                Msg.To.CopyTo(Packet + 27 + Msg.From.Length);
                Msg.Message.CopyTo(Packet + 29 + Msg.From.Length + Msg.To.Length);
            }
            return Buffer;
        }
        public static implicit operator MessagePacket(byte* Pointer)
        {
            MessagePacket Msg = new MessagePacket();
            Msg.Color = *((uint*)(Pointer + 4));
            Msg.ChatType = *((Types*)(Pointer + 8));
            Msg.dwParam = *((uint*)(Pointer + 12));
            Msg.SenderModel = *((uint*)(Pointer + 20));
            Msg.From = new string((sbyte*)(Pointer + 26), 0, *(Pointer + 25));
            Msg.To = new string((sbyte*)(Pointer + 27 + Msg.From.Length), 0, *(Pointer + 26 + Msg.From.Length));
            Msg.Message = new string((sbyte*)(Pointer + 29 + Msg.From.Length + Msg.To.Length), 0, *(Pointer + 28 + Msg.From.Length + Msg.To.Length));
            return Msg;
        }
    }
}
